package com.qf.controller;

import com.qf.pojo.Cart;
import com.qf.pojo.CartItem;
import com.qf.pojo.Product;
import com.qf.pojo.User;
import com.qf.service.ProductService;
import com.qf.service.impl.ProductServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * 购物车的后端地址
 */
@WebServlet("/cart")
public class CartServlet extends BaseServlet {


    //定义一个功能:获取购物车Cart,如果从HttpSession中获取到的购物车是null,没有就需要存储到HttpSession中
    public Cart getCart(HttpServletRequest request,HttpServletResponse response){
        //1)从request对象中获取HttpSession
        HttpSession session = request.getSession();
        //2)从HttpSession中取出购物车的数据
        Cart cart = (Cart) session.getAttribute("cart") ;
        //3)判断如果获取到cart是null
        if(cart==null){
            System.out.println("购物车为空...");
            //HttpSession没有,
            //创建购物车
            cart = new Cart() ;
            //创建的购物车存储在HttpSession
            session.setAttribute("cart",cart) ;
            String id = session.getId();  //Session依赖的Cookie的名称"JESSIONID" 对应的 id值
            System.out.println(id);
            //设置cookie
            Cookie cookie = new Cookie("JSESSIONID",id) ;
            //存储有效期
            cookie.setMaxAge(60*60*24*7);
            //响应给浏览器
            response.addCookie(cookie);

        }
        System.out.println("购物车不为空...");
        return  cart ;
    }


    /**
     * 将购物车添加到购物车
     * @param request 请求
     * @param response 响应
     */
    public void addCart(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //从request对象中HttpSession,判断是否有用户数据
        HttpSession session = request.getSession();
        User user  = (User) session.getAttribute("user");
        if(user==null){
            //重定向到登录页面
            response.sendRedirect(request.getContextPath()+"/jsp/login.jsp") ;
            return;
        }


        //商品pid/商品数量
        String pid = request.getParameter("pid");
        int count = Integer.parseInt(request.getParameter("count"));
        //System.out.println(pid+"---"+count) ;

        //通过商品pid获取商品实体
        //调用商品业务接口
        ProductService productService = new ProductServiceImpl() ;
        Product product = productService.getProduct(pid);

        //封装购物车项
        CartItem cartItem = new CartItem() ;
        cartItem.setProduct(product) ;
        cartItem.setCount(count) ;


        //从HttpSession中获取购物车
        Cart cart = getCart(request,response);
        //调用购物车中添加购物车项的功能
        cart.addCartItemToCart(cartItem) ;

        //重定向到/jsp/cart.jsp
        response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");


    }

    /**
     * 删除购物车项
     * @param request 请求对象
     * @param response 响应对象
     */
    public void removeCartItem(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //获取商品pid
        String pid = request.getParameter("pid");

        /*//从session获取购物车
        HttpSession session = request.getSession();
        Cart cart = (Cart) session.getAttribute("cart");*/
        Cart cart = getCart(request, response);

        //调用购物车的功能
        cart.removeCartItemFromCart(pid);

        //重定向到/jsp/cart.jsp
        response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
    }

    /**
     * 清空购物车的后端方法
     * @param request
     * @param response
     */
    public void clearCart(HttpServletRequest request,HttpServletResponse response) throws IOException {

        //从HttpSession获取购物车
       /* HttpSession session = request.getSession();
        Cart cart = (Cart)session.getAttribute("cart");*/
        Cart cart = getCart(request, response);

        //调用购物车的方法
        cart.clear() ;


        //将购物车从Session中干掉
       request.getSession().removeAttribute("cart");

        //重定向购物车页面
        response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
    }
}
